package com.zcf.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zcf.common.json.Body;
import com.zcf.common.result.PageResult;
import com.zcf.common.result.ResultVo;
import com.zcf.mapper.AddressMapper;
import com.zcf.pojo.Address;
import com.zcf.service.AddressService;
import com.zcf.utils.Hutool;
import com.zcf.vo.in.PageVo;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

	@Autowired
	private AddressMapper addressMapper;

	public Body addAddress(String userId, String address, String phone, String name, String detail) {
		Address address2 = new Address();
		EntityWrapper<Address> ew = new EntityWrapper<>();
		ew.eq("address", address);
		ew.eq("user_id", userId);
		int count = address2.selectCount(ew);
		if (count > 0) {
			return Body.newInstance("地址已存在");
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		Address add = new Address();
		add.setId(Hutool.getId());
		add.setPhone(phone);
		add.setAddress(address);
		add.setName(name);
		add.setTime(time);
		add.setDetail(detail);
		add.setUserId(userId);
		Integer insert = addressMapper.insert(add);
		if (insert > 0) {
			return Body.newInstance(add);
		}
		return Body.newInstance(501, "上传地址有误");
	}

	public Body upAddress(String id, String userId, String address, String phone, String name, String detail) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		Address add = new Address();
		add.setId(id);
		Address one = addressMapper.selectById(add);
		if (one != null && !"".equals(one)) {
			one.setUserId(userId);
			one.setAddress(address);
			one.setDetail(detail);
			one.setName(name);
			one.setTime(time);
			one.setPhone(phone);
			Integer b = addressMapper.updateById(one);
			if (b > 0) {
				return Body.newInstance(one);
			}
			return Body.newInstance(501, "修改有误");
		}
		return Body.newInstance(501, "未有此条地址");
	}

	public ResultVo addressList(PageVo pv, String userId) {
//		List<Address> list = addressMapper.addressList(userId);
		if (pv.getPage() != null && pv.getPageSize() != null) {
			PageHelper.startPage(pv.getPage(), pv.getPageSize());
		}
		PageResult pr = PageResult.result(addressMapper.addressList(userId));
		ResultVo rv = new ResultVo();
		rv.setList(pr.getRows());
		rv.setTotal(pr.getTotal());
		return rv;
	}

	public Body setMoren(String userId, String id) {
		Address address = new Address();
		address.setId(id);
		address = address.selectById();
		if (address == null) {
			return Body.newInstance("地址不存在");
		}
		EntityWrapper<Address> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		ew.eq("type", 0);
		Integer flag = addressMapper.update(userId);
		if (flag > 0) {
			address.setType(1);
			address.setUserId(userId);
			Boolean fa = address.updateById();
			if (fa) {
				return Body.newInstance("设置成功");
			}
			return Body.newInstance("设置失败");
		}
		return Body.newInstance("设置失败");
	}

}
